#include "CommandProcesser.h"

#include <Encrypt/SM4Encrypt.h>

CommandProcesser::CommandProcesser(QObject *parent)
    : QObject(parent)
{
    m_isDecrypt = false;
}

void CommandProcesser::registerCmd(int functionCode, QMetaObject cmdMetaObject)
{
    m_cmdMultiMap.insert(functionCode,cmdMetaObject);
}

void CommandProcesser::registerProtocol(BaseProtocol *protocol)
{
    m_protocolList.append(protocol);
}
bool CommandProcesser::isDecrypt() const
{
    return m_isDecrypt;
}

void CommandProcesser::setIsDecrypt(bool isDecrypt)
{
    m_isDecrypt = isDecrypt;
}


QList<QSharedPointer<BaseCmd> > CommandProcesser::receiveData(QByteArray data)
{
    int i=0;

    QList<QSharedPointer<BaseCmd> > cmdList;
   /*  {
        // qDebug()<<"CommandProcesser::receiveData~~";
        QString str;
        for(int i=0;i<data.size();i++)
        {
            str+=QString("%1").arg((unsigned char)data.at(i),2,16,QLatin1Char('0'))+' ';
        }
        qDebug()<<str;  //打印十六进制数据

    }*/
    qDebug()<<"CommandProcesser::receiveData"<<data;
    foreach (BaseProtocol* protocol, m_protocolList)
    {
        i++;
       // qDebug()<<" CommandProcesser::receiveData  protocoltype-"<<protocol->protocolType();
        QList<Package> packageList =  protocol->parseData(data);
        foreach (Package package, packageList)
        {

            int functionCode = package.function();
     //       qDebug()<<"functionCode="<<functionCode;
            if(!m_cmdMultiMap.contains(functionCode))
            {
               qDebug()<<"Error:Function code " + QString::number(functionCode,16) + " has no corresponding command!";
                continue;
            }
            qDebug()<<"parseData success " + QString::number(protocol->protocolType(),16);
            QList<QMetaObject> metaList = m_cmdMultiMap.values(functionCode);
            foreach (const QMetaObject &meta, metaList)
            {
//
              //  qDebug()<<"foreach meta now";
                BaseCmd *pCmd=static_cast<BaseCmd*>(meta.newInstance());
                 qDebug()<<"foreach meta now " + QString::number(pCmd->protocolType(),16)+","+QString::number(protocol->protocolType(),16);
                if(pCmd->protocolType() == protocol->protocolType())
                {
                   printf("pass cmd %s\r\n",meta.className());
                   qDebug()<<"protocolType  %s\r\n"<<pCmd-> protocolType();

                    pCmd->setIsDecrypt(m_isDecrypt);
                    if(pCmd->protocolType()==0){
                        QByteArray body2;//
                        body2= package.body();

                        SM4Encrypt *encrypt = SM4Encrypt::getInstance();

                        //encrypt->print16FormatData(body2);

                        body2 =encrypt->DecryptBySM4(body2);

                        package.setBody(body2);

                    }
                     pCmd->unPackage(package);
               //     qDebug()<<"package ";
                    QSharedPointer<BaseCmd> sharedCmd(pCmd);
                    cmdList.append(sharedCmd);
                    qDebug()<<"gogogogo";
                }
            }

        }
    }
    i=0;
    return cmdList;

}

